<html>
<head><meta charset="utf-8"><title>issue 58053 in polonius · t-compiler/wg-polonius · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/186049-t-compiler/wg-polonius/index.html">t-compiler/wg-polonius</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/186049-t-compiler/wg-polonius/topic/issue.2058053.20in.20polonius.html">issue 58053 in polonius</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="188561039"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/186049-t-compiler/wg-polonius/topic/issue%2058053%20in%20polonius/near/188561039" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aaron Weiss <a href="https://rust-lang.github.io/zulip_archive/stream/186049-t-compiler/wg-polonius/topic/issue.2058053.20in.20polonius.html#188561039">(Feb 19 2020 at 16:40)</a>:</h4>
<p>Hey folks,</p>
<p>Sorry if this isn't quite the right place to ask, but I've been going through the nll test suite and I'm curious about how issue 58053 would go through Polonius. Does this still error? It seems a bit odd to me that the type checker doesn't figure out that the origin of the result is the same as the origin of the argument.</p>
<p>I have a playground link here: <a href="https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=8f967ccd6b534b992ea48ff9a4f451c2" target="_blank" title="https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=8f967ccd6b534b992ea48ff9a4f451c2">https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=8f967ccd6b534b992ea48ff9a4f451c2</a></p>



<a name="188565981"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/186049-t-compiler/wg-polonius/topic/issue%2058053%20in%20polonius/near/188565981" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lqd <a href="https://rust-lang.github.io/zulip_archive/stream/186049-t-compiler/wg-polonius/topic/issue.2058053.20in.20polonius.html#188565981">(Feb 19 2020 at 17:34)</a>:</h4>
<p>I think it would error similarly, at least right now: it was my impression that properly handling closures would require higher-rankedness via chalk, but Niko/Matthew would probably know better than me :)</p>



<a name="188566599"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/186049-t-compiler/wg-polonius/topic/issue%2058053%20in%20polonius/near/188566599" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lqd <a href="https://rust-lang.github.io/zulip_archive/stream/186049-t-compiler/wg-polonius/topic/issue.2058053.20in.20polonius.html#188566599">(Feb 19 2020 at 17:42)</a>:</h4>
<p>(similarly, extracting <code>f</code> to a function would make it compile under today's NLL)</p>



<a name="188568908"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/186049-t-compiler/wg-polonius/topic/issue%2058053%20in%20polonius/near/188568908" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lqd <a href="https://rust-lang.github.io/zulip_archive/stream/186049-t-compiler/wg-polonius/topic/issue.2058053.20in.20polonius.html#188568908">(Feb 19 2020 at 18:06)</a>:</h4>
<p>(and I believe this should be solved when higher-ranked lifetimes on closures are supported, like <a href="https://github.com/rust-lang/rust/issues/55526" target="_blank" title="https://github.com/rust-lang/rust/issues/55526">https://github.com/rust-lang/rust/issues/55526</a> or <a href="https://github.com/rust-lang/rust/issues/58052" target="_blank" title="https://github.com/rust-lang/rust/issues/58052">https://github.com/rust-lang/rust/issues/58052</a>)</p>



<a name="188571019"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/186049-t-compiler/wg-polonius/topic/issue%2058053%20in%20polonius/near/188571019" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aaron Weiss <a href="https://rust-lang.github.io/zulip_archive/stream/186049-t-compiler/wg-polonius/topic/issue.2058053.20in.20polonius.html#188571019">(Feb 19 2020 at 18:30)</a>:</h4>
<p><span class="user-mention" data-user-id="116113">@lqd</span> so, if I'm understanding correctly, the issue is basically that f and g should have a type like <code>fn&lt;'a&gt;(&amp;'a i32) -&gt; &amp;'a i32</code>, but are instead getting some type without the quantification?</p>



<a name="188571086"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/186049-t-compiler/wg-polonius/topic/issue%2058053%20in%20polonius/near/188571086" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aaron Weiss <a href="https://rust-lang.github.io/zulip_archive/stream/186049-t-compiler/wg-polonius/topic/issue.2058053.20in.20polonius.html#188571086">(Feb 19 2020 at 18:31)</a>:</h4>
<p>it's a little weird because I've encountered other scenarios like that, and in Oxide, annotating them with local (rather than polymorphic) provenances leads to the same behavior as Rust, but in this case, I get different behavior (Oxide seems to admit the program even with local provenances).</p>



<a name="188571194"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/186049-t-compiler/wg-polonius/topic/issue%2058053%20in%20polonius/near/188571194" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aaron Weiss <a href="https://rust-lang.github.io/zulip_archive/stream/186049-t-compiler/wg-polonius/topic/issue.2058053.20in.20polonius.html#188571194">(Feb 19 2020 at 18:32)</a>:</h4>
<p>If f and g are indeed getting not-quantified lifetimes/provenances/origins, then I'll have to figure out why I'm not producing the same error in that case.</p>



<a name="188574814"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/186049-t-compiler/wg-polonius/topic/issue%2058053%20in%20polonius/near/188574814" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lqd <a href="https://rust-lang.github.io/zulip_archive/stream/186049-t-compiler/wg-polonius/topic/issue.2058053.20in.20polonius.html#188574814">(Feb 19 2020 at 19:12)</a>:</h4>
<p>Unfortunately I’m not sure of the specifics (there was also another case where quantification was short circuited as ‘static) but for the NLL case maybe <span class="user-mention" data-user-id="116083">@pnkfelix</span> could help you there for instance</p>



<a name="189056624"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/186049-t-compiler/wg-polonius/topic/issue%2058053%20in%20polonius/near/189056624" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/186049-t-compiler/wg-polonius/topic/issue.2058053.20in.20polonius.html#189056624">(Feb 25 2020 at 20:15)</a>:</h4>
<p><span class="user-mention" data-user-id="127262">@Aaron Weiss</span> the tl;dr is that this is outside of polonius</p>



<a name="189056684"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/186049-t-compiler/wg-polonius/topic/issue%2058053%20in%20polonius/near/189056684" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/186049-t-compiler/wg-polonius/topic/issue.2058053.20in.20polonius.html#189056684">(Feb 25 2020 at 20:16)</a>:</h4>
<p>at least <em>at present</em>, the compiler determines the signatures of functions like that in its initial phases of type inference</p>



<a name="189056690"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/186049-t-compiler/wg-polonius/topic/issue%2058053%20in%20polonius/near/189056690" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/186049-t-compiler/wg-polonius/topic/issue.2058053.20in.20polonius.html#189056690">(Feb 25 2020 at 20:16)</a>:</h4>
<p>and polonius (and NLL checker, etc) comes later and is just <em>given</em> that signature</p>



<a name="189056698"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/186049-t-compiler/wg-polonius/topic/issue%2058053%20in%20polonius/near/189056698" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/186049-t-compiler/wg-polonius/topic/issue.2058053.20in.20polonius.html#189056698">(Feb 25 2020 at 20:16)</a>:</h4>
<p>now, it is true that we could probably do better</p>



<a name="189056730"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/186049-t-compiler/wg-polonius/topic/issue%2058053%20in%20polonius/near/189056730" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/186049-t-compiler/wg-polonius/topic/issue.2058053.20in.20polonius.html#189056730">(Feb 25 2020 at 20:17)</a>:</h4>
<p>and I'm a bit surprised that that test case doesn't work, though I remember now that <span class="user-mention" data-user-id="116083">@pnkfelix</span> and I were talking about this at some point and I had forgotten about it</p>



<a name="189056747"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/186049-t-compiler/wg-polonius/topic/issue%2058053%20in%20polonius/near/189056747" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/186049-t-compiler/wg-polonius/topic/issue.2058053.20in.20polonius.html#189056747">(Feb 25 2020 at 20:17)</a>:</h4>
<p>we don't support a kind of "fully explicit" way of annotating the types on closures, which is kind of annoying</p>



<a name="189063819"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/186049-t-compiler/wg-polonius/topic/issue%2058053%20in%20polonius/near/189063819" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aaron Weiss <a href="https://rust-lang.github.io/zulip_archive/stream/186049-t-compiler/wg-polonius/topic/issue.2058053.20in.20polonius.html#189063819">(Feb 25 2020 at 21:37)</a>:</h4>
<p><span class="user-mention" data-user-id="116009">@nikomatsakis</span> I figured that it was related to inference, but I can't figure out an annotated version of the program that I can explain why it produces an error in terms of how Oxide works (or how Polonius seems to work).</p>



<a name="189063852"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/186049-t-compiler/wg-polonius/topic/issue%2058053%20in%20polonius/near/189063852" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aaron Weiss <a href="https://rust-lang.github.io/zulip_archive/stream/186049-t-compiler/wg-polonius/topic/issue.2058053.20in.20polonius.html#189063852">(Feb 25 2020 at 21:37)</a>:</h4>
<p>I tried to annotate it like so:</p>
<div class="codehilite"><pre><span></span><span class="k">fn</span> <span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">tmp</span>: <span class="kt">i32</span> <span class="o">=</span><span class="w"> </span><span class="mi">3</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">i</span>: <span class="kp">&amp;</span><span class="na">&#39;t0</span><span class="w"> </span><span class="kt">i32</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">&amp;</span><span class="n">tmp</span><span class="p">;</span><span class="w"></span>

<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">f</span>: <span class="nc">fn</span><span class="p">(</span><span class="o">&amp;</span><span class="na">&#39;f1</span><span class="w"> </span><span class="kt">i32</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="kp">&amp;</span><span class="na">&#39;f2</span><span class="w"> </span><span class="kt">i32</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">|</span><span class="n">x</span>: <span class="kp">&amp;</span><span class="na">&#39;f1</span><span class="w"> </span><span class="kt">i32</span><span class="o">|</span><span class="w"> </span>-&gt; <span class="kp">&amp;</span><span class="na">&#39;f2</span><span class="w"> </span><span class="kt">i32</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="p">};</span><span class="w"></span>
<span class="w">    </span><span class="c1">//~^ ERROR lifetime may not live long enough</span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">j</span>: <span class="kp">&amp;</span><span class="na">&#39;t0</span><span class="w"> </span><span class="kt">i32</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">f</span><span class="p">(</span><span class="n">i</span><span class="p">);</span><span class="w"></span>

<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">g</span>: <span class="nc">fn</span><span class="p">(</span><span class="o">&amp;</span><span class="na">&#39;x1</span><span class="w"> </span><span class="kt">i32</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="kp">&amp;</span><span class="na">&#39;x2</span><span class="w"> </span><span class="kt">i32</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">|</span><span class="n">x</span>: <span class="kp">&amp;</span><span class="na">&#39;x1</span><span class="w"> </span><span class="kt">i32</span><span class="o">|</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="p">};</span><span class="w"></span>
<span class="w">    </span><span class="c1">//~^ ERROR lifetime may not live long enough</span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">k</span>: <span class="kp">&amp;</span><span class="na">&#39;t0</span><span class="w"> </span><span class="kt">i32</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">g</span><span class="p">(</span><span class="n">i</span><span class="p">);</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</pre></div>



<a name="189063966"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/186049-t-compiler/wg-polonius/topic/issue%2058053%20in%20polonius/near/189063966" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aaron Weiss <a href="https://rust-lang.github.io/zulip_archive/stream/186049-t-compiler/wg-polonius/topic/issue.2058053.20in.20polonius.html#189063966">(Feb 25 2020 at 21:38)</a>:</h4>
<p>But from my perspective, since these lifetime variables are local to the scope we're in, it's fine to equate them (by flowing loans from e.g. 'f1 into 'f2).</p>



<a name="189064034"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/186049-t-compiler/wg-polonius/topic/issue%2058053%20in%20polonius/near/189064034" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aaron Weiss <a href="https://rust-lang.github.io/zulip_archive/stream/186049-t-compiler/wg-polonius/topic/issue.2058053.20in.20polonius.html#189064034">(Feb 25 2020 at 21:39)</a>:</h4>
<p>Is this because there's some other more syntactic check that actually happens here _before_ getting into the borrow checker (regardless of its implementation)?</p>



<a name="189064043"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/186049-t-compiler/wg-polonius/topic/issue%2058053%20in%20polonius/near/189064043" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aaron Weiss <a href="https://rust-lang.github.io/zulip_archive/stream/186049-t-compiler/wg-polonius/topic/issue.2058053.20in.20polonius.html#189064043">(Feb 25 2020 at 21:39)</a>:</h4>
<p>If so, what is that syntactic check doing exactly?</p>



<a name="189064162"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/186049-t-compiler/wg-polonius/topic/issue%2058053%20in%20polonius/near/189064162" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aaron Weiss <a href="https://rust-lang.github.io/zulip_archive/stream/186049-t-compiler/wg-polonius/topic/issue.2058053.20in.20polonius.html#189064162">(Feb 25 2020 at 21:41)</a>:</h4>
<p>(also, please forgive my abuse-of-notation that is using unboxed function types for closures)</p>



<a name="189629383"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/186049-t-compiler/wg-polonius/topic/issue%2058053%20in%20polonius/near/189629383" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/186049-t-compiler/wg-polonius/topic/issue.2058053.20in.20polonius.html#189629383">(Mar 03 2020 at 20:09)</a>:</h4>
<p>yeah, a solid question <span class="user-mention" data-user-id="127262">@Aaron Weiss</span> -- I have to go look -- I remember when I was last looking at this question that it seemed to be inferring a type like <code>fn(&amp;'a) -&gt; &amp;'b</code>, which wouldn't really be legal to type even</p>



<a name="189826274"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/186049-t-compiler/wg-polonius/topic/issue%2058053%20in%20polonius/near/189826274" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/186049-t-compiler/wg-polonius/topic/issue.2058053.20in.20polonius.html#189826274">(Mar 05 2020 at 19:17)</a>:</h4>
<blockquote>
<p>we don't support a kind of "fully explicit" way of annotating the types on closures, which is kind of annoying</p>
</blockquote>
<p>Can you use the trick of accepting and returning the closure?</p>
<div class="codehilite"><pre><span></span><span class="k">fn</span> <span class="nf">restrict</span><span class="p">(</span><span class="n">f</span>: <span class="nc">F</span><span class="p">)</span><span class="w"></span>
<span class="k">where</span><span class="w"></span>
<span class="w">    </span><span class="n">F</span>: <span class="nb">Fn</span><span class="p">(</span><span class="cm">/* All the specifics you need */</span><span class="p">)</span><span class="w"></span>
<span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">f</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">fn</span> <span class="nf">usage</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">f</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">restrict</span><span class="p">(</span><span class="o">||</span><span class="w"> </span><span class="cm">/* ... */</span><span class="p">);</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</pre></div>



<a name="189842426"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/186049-t-compiler/wg-polonius/topic/issue%2058053%20in%20polonius/near/189842426" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lqd <a href="https://rust-lang.github.io/zulip_archive/stream/186049-t-compiler/wg-polonius/topic/issue.2058053.20in.20polonius.html#189842426">(Mar 05 2020 at 22:08)</a>:</h4>
<p>yeah that should work here</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>